/* -------------------------------------------------------------------------- *
   TestWinUnit - Maria Blees (maria.blees@microsoft.com)

   File: PlainTextLoggerTest.cpp

   Contains tests for the PlainTextLogger class.  These are verifiable
   by inspection only.
 * -------------------------------------------------------------------------- */
#include "WinUnit.h"

#include "TestablePlainTextLogger.h"

#pragma warning(push)
#pragma warning(disable : 4702)

BEGIN_TEST(PlainTextLoggerOutputTestExecutableNamePreTestWithVerbosityGreaterThanZero)
{
    TestablePlainTextLogger logger;
    logger.SetVerbosity(1);

    WIN_TRACE("INFO: Next line should indicate executable name about to be processed (not full path).\n");
    logger.OutputTestExecutableNamePreTest(L"full path to executable", L"executable name");
    WIN_TRACE("OUTPUT: %s", logger.GetBuffer());
}
END_TEST

BEGIN_TEST(PlainTextLoggerOutputTestExecutableNamePreTestWithVerbosityEqualsZero)
{
    TestablePlainTextLogger logger;
    logger.SetVerbosity(0);

    WIN_TRACE("INFO: Next line should be an empty line of output.\n");
    logger.OutputTestExecutableNamePreTest(L"full path to executable", L"executable name");
    WIN_TRACE("OUTPUT: %s\n", logger.GetBuffer());
}
END_TEST

BEGIN_TEST(PlainTextLoggerOutputTestExecutableNamePostTestWithNoTestsRun)
{
    TestablePlainTextLogger logger;
    logger.SetVerbosity(0);

    WIN_TRACE("INFO: Next line should indicate executable name (not full path) and that no tests were run.\n");
    logger.OutputTestExecutableNamePostTest(L"full path to executable", L"executable name", 0, 0);
    WIN_TRACE("OUTPUT: %s", logger.GetBuffer());
}
END_TEST

BEGIN_TEST(PlainTextLoggerOutputTestExecutableNamePostTestWithSomeTestsFailedAndVerbosityGreaterThanZero)
{
    TestablePlainTextLogger logger;
    logger.SetVerbosity(1);

    WIN_TRACE("INFO: Next line should indicate executable name (not full path) and that 3 of 4 tests failed.\n");
    logger.OutputTestExecutableNamePostTest(L"full path to executable", L"executable name", 1, 4);
    WIN_TRACE("OUTPUT: %s", logger.GetBuffer());
}
END_TEST

BEGIN_TEST(PlainTextLoggerOutputTestExecutableNamePostTestWithSomeTestsFailedAndVerbosityEqualsZero)
{
    TestablePlainTextLogger logger;
    logger.SetVerbosity(0);

    WIN_TRACE("INFO: Next line should indicate executable name (not full path) and that 3 of 4 tests failed.\n");
    logger.OutputTestExecutableNamePostTest(L"full path to executable", L"executable name", 1, 4);
    WIN_TRACE("OUTPUT: %s", logger.GetBuffer());
}
END_TEST

BEGIN_TEST(PlainTextLoggerOutputTestExecutableNamePostTestWithNoTestsFailedAndVerbosityGreaterThanZero)
{
    TestablePlainTextLogger logger;
    logger.SetVerbosity(1);

    WIN_TRACE("INFO: Next line should indicate executable name (not full path) and that 4 of 4 tests succeeded.\n");
    logger.OutputTestExecutableNamePostTest(L"full path to executable", L"executable name", 4, 4);
    WIN_TRACE("OUTPUT: %s", logger.GetBuffer());
}
END_TEST

BEGIN_TEST(PlainTextLoggerOutputTestExecutableNamePostTestWithNoTestsFailedAndVerbosityEqualsZero)
{
    TestablePlainTextLogger logger;
    logger.SetVerbosity(0);

    WIN_TRACE("INFO: Next line should be empty output.\n");
    logger.OutputTestExecutableNamePostTest(L"full path to executable", L"executable name", 4, 4);
    WIN_TRACE("OUTPUT: %s\n", logger.GetBuffer());
}
END_TEST

BEGIN_TEST(PlainTextLoggerOutputTestNamePreTestWithVerbosityGreaterThanZero)
{
    TestablePlainTextLogger logger;
    logger.SetVerbosity(1);

    WIN_TRACE("INFO: Next line should indicate test name.\n");
    logger.OutputTestNamePreTest("test name");
    WIN_TRACE("OUTPUT: %s", logger.GetBuffer());
}
END_TEST

BEGIN_TEST(PlainTextLoggerOutputTestNamePreTestWithVerbosityEqualsZero)
{
    TestablePlainTextLogger logger;
    logger.SetVerbosity(0);

    WIN_TRACE("INFO: Next line should be empty output.\n");
    logger.OutputTestNamePreTest("test name");
    WIN_TRACE("OUTPUT: %s\n", logger.GetBuffer());
}
END_TEST

BEGIN_TEST(PlainTextLoggerOutputTestNamePostTestSucceededWithVerbosityGreaterThanOne)
{
    TestablePlainTextLogger logger;
    logger.SetVerbosity(2);

    WIN_TRACE("INFO: Next line should indicate test succeeded.\n");
    logger.OutputTestNamePostTest("test name", true);
    WIN_TRACE("OUTPUT: %s", logger.GetBuffer());
}
END_TEST

BEGIN_TEST(PlainTextLoggerOutputTestNamePostTestSucceededWithVerbosityEqualsOne)
{
    TestablePlainTextLogger logger;
    logger.SetVerbosity(1);

    WIN_TRACE("INFO: Next line should be empty output.\n");
    logger.OutputTestNamePostTest("test name", true);
    WIN_TRACE("OUTPUT: %s\n", logger.GetBuffer());
}
END_TEST

BEGIN_TEST(PlainTextLoggerOutputTestNamePostTestFailedWithVerbosityGreaterThanOne)
{
    TestablePlainTextLogger logger;
    logger.SetVerbosity(2);

    WIN_TRACE("INFO: Next line should indicate test failed.\n");
    logger.OutputTestNamePostTest("test name", false);
    WIN_TRACE("OUTPUT: %s", logger.GetBuffer());
}
END_TEST

BEGIN_TEST(PlainTextLoggerOutputTestNamePostTestFailedWithVerbosityEqualsOne)
{
    TestablePlainTextLogger logger;
    logger.SetVerbosity(1);

    WIN_TRACE("INFO: Next line should indicate test failed.\n");
    logger.OutputTestNamePostTest("test name", false);
    WIN_TRACE("OUTPUT: %s", logger.GetBuffer());
}
END_TEST

BEGIN_TEST(PlainTextLoggerOutputTraceWithVerbosityGreaterThanZero)
{
    TestablePlainTextLogger logger;
    logger.SetVerbosity(1);

    WIN_TRACE("INFO: Next line should indicate trace statement.\n");
    logger.OutputTrace(L"trace statement\n");
    WIN_TRACE("OUTPUT: %s", logger.GetBuffer());
}
END_TEST

BEGIN_TEST(PlainTextLoggerOutputTraceWithVerbosityEqualsZero)
{
    TestablePlainTextLogger logger;
    logger.SetVerbosity(0);

    WIN_TRACE("INFO: Next line should be empty output.\n");
    logger.OutputTrace(L"trace statement\n");
    WIN_TRACE("OUTPUT: %s\n", logger.GetBuffer());
}
END_TEST

BEGIN_TEST(PlainTextLoggerOutputTestError)
{
    TestablePlainTextLogger logger;
    logger.SetVerbosity(0);

    WIN_TRACE("INFO: Next line should indicate test error.\n");
    logger.OutputTestError(L"test error");
    WIN_TRACE("OUTPUT: %s", logger.GetBuffer());
}
END_TEST

BEGIN_TEST(PlainTextLoggerOutputProgramErrorLine)
{
    // Program errors always show up regardless of verbosity.
    TestablePlainTextLogger logger;
    logger.SetVerbosity(0);

    WIN_TRACE("INFO: Next line should indicate program error.\n");
    logger.OutputProgramErrorLine(L"msg1 ", L"msg2 ", L"msg3 ", L"msg4 ");
    WIN_TRACE("OUTPUT: %s", logger.GetBuffer());
}
END_TEST

BEGIN_TEST(PlainTextLoggerOutputFinalResultsNoTestsRun)
{
    // Final test results always show up regardless of verbosity.
    TestablePlainTextLogger logger;
    logger.SetVerbosity(0);

    WIN_TRACE("INFO: Next line should indicate no tests were found.\n");
    logger.OutputFinalResults(0, 0);

    char delimiter[] = "\n";
    char* context = NULL;
    const char* line = strtok_s(logger.GetBuffer(), delimiter, &context);
    while(line != NULL)
    {
        WIN_TRACE("OUTPUT: %s\n", line);
        line = strtok_s(NULL, delimiter, &context);
    }
}
END_TEST

BEGIN_TEST(PlainTextLoggerOutputFinalResultsSomeTestsFailed)
{
    // Final test results always show up regardless of verbosity.
    TestablePlainTextLogger logger;
    logger.SetVerbosity(0);

    WIN_TRACE("INFO: Next two lines should indicate 3 of 4 tests failed.\n");
    logger.OutputFinalResults(1, 4);

    char delimiter[] = "\n";
    char* context = NULL;
    const char* line = strtok_s(logger.GetBuffer(), delimiter, &context);
    while(line != NULL)
    {
        WIN_TRACE("OUTPUT: %s\n", line);
        line = strtok_s(NULL, delimiter, &context);
    }
}
END_TEST

BEGIN_TEST(PlainTextLoggerOutputFinalResultsAllTestsSucceeded)
{
    // Final test results always show up regardless of verbosity.
    TestablePlainTextLogger logger;
    logger.SetVerbosity(0);

    WIN_TRACE("INFO: Next two lines should indicate 4 tests succeeded.\n");
    logger.OutputFinalResults(4, 4);

    char delimiter[] = "\n";
    char* context = NULL;
    const char* line = strtok_s(logger.GetBuffer(), delimiter, &context);
    while(line != NULL)
    {
        WIN_TRACE("OUTPUT: %s\n", line);
        line = strtok_s(NULL, delimiter, &context);
    }
}
END_TEST

#pragma warning(pop)